import java.util.ArrayList;

/**
 * central actuator between server and client
 * 
 * @author Ronny and Johannes
 * 
 */
public class GUIController {

	public final static int roomX = 10, roomY = 10;
	private KomMap kommap;
	private HTTPConnection httpConnection = new HTTPConnection();

	public GUIController(KomMap kommap) {
		this.kommap = kommap;
	}

	private PositionXY xyCoordinationCal(PositionXY pos) {
		PositionXY posPixel = new PositionXY();

		posPixel = pos;

		return posPixel;
	}

	//TODO rename
	/**
	 * Send the command to get the coordinates from the current position which are stored at the server
	 */
	public void getXYCoordinates() {
		String spotID = "0000";
		// PositionXY pos= httpConnection.httpTestGetXYCoordinates(spotID);
		// xyCoordinationCal(pos);
		PositionXY pos = httpConnection.httpGetXYCoordinates(spotID);

		// our gui xmax=770,ymax=620
		// pos.setX(roomX*pos.getX()/770);
		// pos.setX(roomY*pos.getY()/620);

		kommap.komMapPane.getCoordinatefromServer(xyCoordinationCal(pos));

	}

	//TODO rename
	/**
	 * Send the the corrected coordinates to the server
	 * @param x is the x coordinate
	 * @param y is the y coordinate
	 */
	public void sendDatetoServer(String spotID, int x, int y) {
		httpConnection.httpSendCorrectedXYCoordinates(spotID, x, y);
	}

	/**
	 * Send the command to get all reference points which are stored on the
	 * server
	 */
	public ArrayList<PositionXY> getReferencepoints() {
		ArrayList<PositionXY> positionxy = httpConnection.httpGetReferencePoints();
		return positionxy;
	}

	/**
	 * Send the command to add a sample to a reference point on the server
	 * 
	 * @param x
	 *            is the x coordinate
	 * @param y
	 *            is the y coordinate
	 * @param sampleCount
	 *            is the number how many packets are sampled
	 * @param keepTable
	 *            if true the sample will be attached to the last table and if
	 *            false a new table will be created
	 */
	public void addSamplesToReferencepoint(int x, int y, int sampleCount,
			boolean keepTable) {
		httpConnection.httpAddSampleToReferencePoint(x, y, sampleCount,
				keepTable);
	}

	/**
	 * Send the command to add a reference point to the server
	 * 
	 * @param x
	 *            is the x coordinate
	 * @param y
	 *            is the y coordinate
	 * @param sampleCount
	 *            is the number how many packets are sampled
	 */
	public void addReferencePoint(int x, int y, int sampleCount) {
		httpConnection.httpAddReferencePoint(x, y, sampleCount);
	}

	/**
	 * Send the command to stop the sampling for positioning at the server
	 */
	public void stopPositioning() {

		if (kommap.komconfigpane.timerstarted) {
			kommap.komconfigpane.timer.stop();
			kommap.komconfigpane.timerstarted = false;
		}

		httpConnection.httpStopPositioning();
	}

	/**
	 * Send the command to remove all reference points at the server
	 */
	public void clearAllRefPoints() {
		httpConnection.httpClearAllRefPoints();
	}

	/**
	 * Send the command to remove a reference point at the server
	 * 
	 * @param x
	 *            is the x coordinate
	 * @param y
	 *            is the y coordinate
	 */
	public void removeRefPoint(int x, int y) {
		httpConnection.httpRemoveRefPoint(x, y);
	}
	
//	private int count=0;
	/**
	 * Checks if the server has finished collecting samples
	 * @return false if the server is still collecting samples and true if the server is finished 
	 */
	public boolean isSamplingFinished() {
		return httpConnection.httpisRefPointSamplingFinished();
//		if (count>10){
//			count=0;
//			return true;
			
//		}
//	 try {
//		Thread.sleep(100);
//		 } catch (InterruptedException e) {
//		 // TODO Auto-generated catch block
//		 e.printStackTrace();
//		 }
//		 count++;
//		 return false;
	}

}